import { mergeRsbuildConfig, type RsbuildConfig } from '@rsbuild/core';
import { pluginReact, type PluginReactOptions } from '@rsbuild/plugin-react';
import { pluginSvgr, type PluginSvgrOptions } from "@rsbuild/plugin-svgr";
import { pluginSass, type PluginSassOptions } from "@rsbuild/plugin-sass";
import { pluginLess, type PluginLessOptions } from "@rsbuild/plugin-less";
import { pluginTypeCheck, type PluginTypeCheckerOptions } from '@rsbuild/plugin-type-check';
import { pluginBasicSsl, type PluginBasicSslOptions } from '@rsbuild/plugin-basic-ssl';
import { pluginSourceBuild, type PluginSourceBuildOptions } from '@rsbuild/plugin-source-build';

export * as helper from "./helper";
export * from "@rsbuild/core";
export * from '@rsbuild/plugin-react';
export * from "@rsbuild/plugin-svgr";
export * from '@rsbuild/plugin-type-check';
export * from '@rsbuild/plugin-basic-ssl';
export * from '@rsbuild/plugin-source-build';
const deepmerge = require("deepmerge");

export { deepmerge };
export const isProd = process.env.NODE_ENV === 'production'; // 是否为生成环境打包

interface GetBaseConfigParams {
  rsbuildConfig?: RsbuildConfig,
  pluginBasicSslEnable?: boolean,   // 是否使用插件: @rsbuild/plugin-basic-ssl
  pluginBasicSslOptions?: PluginBasicSslOptions,
  pluginReactEnable?: boolean,   // 是否使用插件: @rsbuild/plugin-react
  pluginReactOptions?: PluginReactOptions,
  pluginSvgrEnable?: boolean,   // 是否使用插件: @rsbuild/plugin-svgr
  pluginSvgrOptions?: PluginSvgrOptions,
  pluginSassEnable?: boolean,   // 是否使用插件: @rsbuild/plugin-sass
  pluginSassOptions?: PluginSassOptions,
  pluginLessEnable?: boolean,   // 是否使用插件: @rsbuild/plugin-less
  pluginLessOptions?: PluginLessOptions,
  pluginTypeCheckEnable?: boolean,   // 是否使用插件: @rsbuild/plugin-type-check
  pluginTypeCheckerOptions?: PluginTypeCheckerOptions,
  pluginSourceBuildEnable?: boolean,   // 是否使用插件: @rsbuild/plugin-source-build
  pluginSourceBuildOptions?: PluginSourceBuildOptions,
};

export const getBaseConfig = (params: GetBaseConfigParams = {}):RsbuildConfig => {
  const { 
    rsbuildConfig = {}, 
    pluginBasicSslEnable = false, 
    pluginBasicSslOptions = {}, 
    pluginReactEnable = true, 
    pluginReactOptions = {},
    pluginSvgrEnable = true, 
    pluginSvgrOptions = {},
    pluginSassEnable = true, 
    pluginSassOptions = {},
    pluginLessEnable = true, 
    pluginLessOptions = {},
    pluginTypeCheckEnable = true, 
    pluginTypeCheckerOptions = {},
    pluginSourceBuildEnable = false,
    pluginSourceBuildOptions = {},
  } = params;
  
  return mergeRsbuildConfig({
    html: {
      template: "./public/index.html"
    },
    // 1. 入口配置
    source: {
      entry: {
        index: './src',
      }
    },
    resolve:{
      alias: {
        '@/': './src/',
        'vendor/': '../vendor/',
      },
    },
    // 开发服务器配置
    server: {
      publicDir: {
        name: "./public",   // 作为静态资源服务的文件夹
        copyOnBuild: false,
      },
      htmlFallback: false,
      historyApiFallback: {
        index: '/index.html',
      },
    },
    output: {
      overrideBrowserslist: [
        // 'chrome >= 87', 'edge >= 88', 'firefox >= 78', 'safari >= 14',
        'chrome >= 100'
      ]
    },
    // 插件配置
    plugins: [
      // react插件true
      pluginReactEnable ? pluginReact(pluginReactOptions) : null,

      // typescript 语法检查,注意语法错误只能在命令行中输出，不能支持less，scss再页面中输出
      pluginTypeCheckEnable ? pluginTypeCheck(deepmerge({
        forkTsCheckerOptions: {
          async: true,
          devServer: true, // 输出到webpack-dev-server服务中
          typescript: {
            memoryLimit: 2048
          }
        }
      }, pluginTypeCheckerOptions)): null,
  
      // svg转为react组件
      pluginSvgrEnable ? pluginSvgr(deepmerge({
        mixedImport: true,
      }, pluginSvgrOptions)) : null,

      // 支持sass，scss
      pluginSassEnable ? pluginSass(pluginSassOptions) : null,

      // 支持less
      pluginLessEnable ? pluginLess(pluginLessOptions) : null,

      // 是否使用自签名的https证书
      pluginBasicSslEnable ? pluginBasicSsl(pluginBasicSslOptions) : null,

      // 是否使用source-build
      pluginSourceBuildEnable ? pluginSourceBuild(pluginSourceBuildOptions) : null,
    ],
  }, rsbuildConfig);
}

